package com.chm.fw.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.chm.fw.bean.AdapterTypeBean;

import java.util.List;

public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter {

    private LayoutInflater inflater;//布局器
    private List<AdapterTypeBean> mData;
    private OnItemClickListener mOnItemClickListener;
    private @LayoutRes
    int[] resources;

    protected BaseRecyclerViewAdapter(Context context, List<AdapterTypeBean> data, OnItemClickListener onItemClickListener, @LayoutRes int... resources) {
        this.inflater = LayoutInflater.from(context);
        this.mData = data;
        this.mOnItemClickListener = onItemClickListener;
        this.resources = resources;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, final int viewType) {
        final View itemView = inflater.inflate(resources[viewType], parent, false);
        final ViewHolder viewHolder = new ViewHolder(itemView);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(itemView, mData.get(position), viewType);
                }
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            onBindViewHolder(viewHolder, mData.get(position), position);
        }
    }

    public abstract void onBindViewHolder(ViewHolder viewHolder, AdapterTypeBean itemData, int position);

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        AdapterTypeBean typeBean = mData.get(position);
        return typeBean.getType();
    }

    public interface OnItemClickListener {
        void onItemClick(View itemView, AdapterTypeBean itemData, int viewType);
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> viewHolder;
        private View view;

        ViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            viewHolder = new SparseArray<>();
        }

        public View get(int id) {
            View childView = viewHolder.get(id);
            if (childView == null) {
                childView = view.findViewById(id);
                viewHolder.put(id, childView);
            }
            return childView;
        }

        public View getConvertView() {
            return view;
        }

        public TextView getTextView(int id) {
            return (TextView) get(id);
        }

        public Button getButton(int id) {
            return (Button) get(id);
        }

        public ImageView getImageView(int id) {
            return (ImageView) get(id);
        }
    }
}
